import '../api/bean/config_bean.dart';
import '../ads/ads_const.dart';
import '../config/app_const.dart';
import '../plugin/ts_plugin.dart';
import '../utils/device_package_info.dart';
import '../utils/json_util.dart';
import '../../utils/k_log.dart';
import '../utils/sp_utils.dart';

class UserSp {
  static UserSp get instance => _getInstance();

  // 静态私有成员，没有初始化
  static UserSp? _instance;

  // 工厂模式 : 单例公开访问点
  factory UserSp() => _getInstance();

  // 静态、同步、私有访问点
  static UserSp _getInstance() {
    return _instance ??= UserSp._init();
  }

  UserSp._init() {
    SpUtils.instance;
  }

  //是否第一次进入App
  putIsFirstApp(bool value) {
    SpUtils.instance.put("isFirstApp", value);
  }

  Future<bool> isFirstApp({Function? callback}) async {
    return await SpUtils.instance.get("isFirstApp", true, callback);
  }

  //是否第一次进入首页
  putIsFirstInHome(bool value) {
    SpUtils.instance.put("isFirstInHome", value);
  }

  Future<bool> isFirstInHome({Function? callback}) async {
    return await SpUtils.instance.get("isFirstInHome", true, callback);
  }

  ///是否显示过领取VIP弹窗
  putIsShowVipDialog(bool value) {
    SpUtils.instance.put("isShowVipDialog", value);
  }

  Future<bool> isShowVipDialog({Function? callback}) async {
    return await SpUtils.instance.get("isShowVipDialog", true, callback);
  }

  ///本地初始化信息
  putAppConfig(String? value) {
    SpUtils.instance.put("appConfig", value);
  }

  Future<String?> _getAppConfigString({Function? callback}) async {
    return await SpUtils.instance.get("appConfig", null, callback);
  }

  ConfigBean? configBean;

  ///保存本地初始化信息
  void saveAppConfig({
    ConfigBean? infoEntity,
  }) {
    if (infoEntity != null) {
      //赋值
      configBean = infoEntity;
      //单独保存djId
      if (infoEntity.djId != null && infoEntity.djId!.isNotEmpty) {
        _putDjid(infoEntity.djId);
      }
      //单独保存riseId
      if (infoEntity.riseId != null &&
          infoEntity.riseId.toString()!.isNotEmpty) {
        _putRiseId(infoEntity.riseId.toString());
      }
      //必须先转化成json字符串
      var data = JsonUtil.encodeObj(infoEntity);
      putAppConfig(data);
    }
  }

  ///获取本地初始化信息
  Future<ConfigBean?>? getAppConfig() async {
    var userInfo = await _getAppConfigString();
    if (userInfo != null && userInfo.isNotEmpty) {
      //赋值
      configBean = JsonUtil.getObject(userInfo, (v) => ConfigBean.fromJson(v));
    } else {
      //默认配置
      configBean = ConfigBean(
        id: 1,
        upgrade: "1.0.0",
        appId: AppConst.djAppId,
        pkgName: await DevicePackageInfo().getPackageName(),
        appName: await DevicePackageInfo().getAppName,
        adSwitch: 0,
        adDelay: 0,
        adChain:
            "[{\"adType\":2,\"interval\":10,\"number\":999},{\"adType\":1,\"interval\":30,\"number\":999},{\"adType\":1,\"interval\":100,\"number\":999},{\"adType\":2,\"interval\":120,\"number\":999},{\"adType\":2,\"interval\":180,\"number\":999},{\"adType\":1,\"interval\":180,\"number\":999}]",
        clickTrigger: 0,
        closeRate: 100,
        checkFlag: 0,
      );
    }
    return configBean;
  }

  ///存OaId
  saveOaId() async {
    var oaid = await TsPlugin.getOaId();
    // if (oaid.isEmpty) {
    //   oaid = await TsPlugin.getOaIdU();
    // }
    SpUtils.instance.put("oaid", oaid);
    KLog.d("getOaId $oaid");
  }

  Future<String> getOaId({Function? callback}) async {
    return await SpUtils.instance.get("oaid", "", callback);
  }

  ///存OaIdU
  saveOaIdU() async {
    var oaidu = await TsPlugin.getOaIdU();
    SpUtils.instance.put("oaidu", oaidu);
    KLog.d("getOaIdU $oaidu");
  }

  Future<String> getOaIdU({Function? callback}) async {
    return await SpUtils.instance.get("oaidu", "", callback);
  }

  ///存OaIdH
  saveOaIdH() async {
    var isHuaweiOrHonorDevice =
        await DevicePackageInfo().isHuaweiOrHonorDevice();
    //是否是华为或者荣耀手机
    if (isHuaweiOrHonorDevice) {
      var oaidu = await TsPlugin.getOaIdH();
      SpUtils.instance.put("oaidh", oaidu);
      KLog.d("getOaIdH $oaidu");
    }
  }

  Future<String> getOaIdH({Function? callback}) async {
    return await SpUtils.instance.get("oaidh", "", callback);
  }

  ///djid
  _putDjid(String? value) {
    SpUtils.instance.put("djid", value);
  }

  Future<String?> getDjid({Function? callback}) async {
    return await SpUtils.instance.get("djid", null, callback);
  }

  ///djid
  _putRiseId(String? value) {
    SpUtils.instance.put("riseId", value);
  }

  Future<String?> getRiseId({Function? callback}) async {
    return await SpUtils.instance.get("riseId", null, callback);
  }

  ///是否同意隐私协议
  putPrivacyAccept(bool value) {
    SpUtils.instance.put("key_privacy_accept", value);
  }

  Future<bool> isPrivacyAccept({Function? callback}) async {
    return await SpUtils.instance.get("key_privacy_accept", false, callback);
  }

  //开屏次数
  putKpCount(int value) {
    SpUtils.instance.put("KpCount", value);
  }

  Future<int> getKpCount({Function? callback}) async {
    return await SpUtils.instance.get("KpCount", 0, callback);
  }

  putKpCountTime(int value) {
    SpUtils.instance.put("KpCountTime", value);
  }

  Future<int> getKpCountTime({Function? callback}) async {
    return await SpUtils.instance.get("KpCountTime", 0, callback);
  }

  //缓存的插屏广告位ID
  putIsertPosids(String value) {
    SpUtils.instance.put("isertPosids", value);
  }

  Future<String> getIsertPosids({Function? callback}) async {
    return await SpUtils.instance.get("isertPosids", "", callback);
  }

  //缓存的激励视频广告位ID
  putRewardPosids(String value) {
    SpUtils.instance.put("rewardPosids", value);
  }

  Future<String> getRewardPosids({Function? callback}) async {
    return await SpUtils.instance.get("rewardPosids", "", callback);
  }

  putShowAdTime(int value) {
    SpUtils.instance.put("showAdTime", value);
  }

  Future<int> getShowAdTime({Function? callback}) async {
    return await SpUtils.instance.get("showAdTime", 0, callback);
  }

  putAddressIP(String value) {
    SpUtils.instance.put("addressIP", value);
  }

  Future<String> getAddressIP({Function? callback}) async {
    return await SpUtils.instance.get("addressIP", "", callback);
  }

  //是否显示过一次定位弹出
  putIsShowLocationDialog(bool value) {
    SpUtils.instance.put("isShowLocationDialog", value);
  }

  Future<bool> isShowLocationDialog({Function? callback}) async {
    return await SpUtils.instance.get("isShowLocationDialog", false, callback);
  }

  Future<String> getWxName({Function? callback}) async {
    return await SpUtils.instance.get("WxName", "", callback);
  }

  Future putWxName(String value) async{
    return await SpUtils.instance.put("WxName", value);
  }

  Future<String> getWxLogo({Function? callback}) async {
    return await SpUtils.instance.get("WxLogo", "", callback);
  }

  Future putWxLogo(String value)async {
    return await SpUtils.instance.put("WxLogo", value);
  }



}
